package CollectionDemo.SetDemo;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Traverse {
    public static void main(String[] args) {
        Set set = new HashSet();
        set.add("a");
        set.add("b");
        set.add("c");
        set.add("d");

        set.add("d");//Set集合中元素不可重复
        System.out.println(set);
        System.out.println("Set的两种遍历：");
        //迭代器Iterator
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next());
        }
        System.out.println();
        System.out.println("--------");
        //foreach遍历
        for (Object obj: set) {
            System.out.print(obj + " ");
        }
        System.out.println();

    }
}
